﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using SinaImgUpload.SainTool.DataEnhance.Sqlite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SinaImgUpload.SainTool.LocalTool
{
    internal class ClearFile
    {
        public struct ImgInfo
        {
            private string SinaImg;
            private string FileName;

            public string SinaImg1 { get => SinaImg; set => SinaImg = value; }
            public string FileName1 { get => FileName; set => FileName = value; }
        }

        /// <summary>
        /// 批量处理文件
        /// </summary>
        /// <param name="FloderPath"></param>
        internal static void AutoSaveListBatch(string FloderPath)
        {
            List<string> FoloderList = Directory.GetFiles(FloderPath, "*.*",
                    SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".lst") || s.EndsWith(".txt")).ToList();
            foreach (string line in FoloderList)
            {
                Console.WriteLine(line);

                AutoSaveList(line);
            }
        }

        public static void AutoSaveList(String FilePath)
        {
            List<ImgInfo> TempList = new List<ImgInfo>();

            foreach (string line in FileHelper.ReadAllLine(FilePath))
            {
                if (line == "") continue;
                string SinaUrl = line;
                string FileName = FileNameHelper.GetFileName(line);

                TempList.Add(new ImgInfo { SinaImg1 = SinaUrl, FileName1 = FileName });
            }
            TempList = VerificationList(TempList);

            string ChiledPath = FileNameHelper.CreateFileNameWithChildFloder(FilePath, "已处理");

            SaveImgInfo(ChiledPath, TempList);
        }

        /// <summary>
        /// 保存信息列表
        /// </summary>
        /// <param name="ChiledPath"></param>
        /// <param name="tempList"></param>
        private static void SaveImgInfo(string ChiledPath, List<ImgInfo> tempList)
        {
            if (tempList.Count < 4)
                return;

            foreach (ImgInfo SingleImgInfo in tempList)
            {
                FileHelper.AppendText(ChiledPath, SingleImgInfo.SinaImg1);
            }
        }

        /// <summary>
        /// 验证图片是否有效
        /// </summary>
        /// <param name="ImgList"></param>
        /// <returns></returns>
        public static List<ImgInfo> VerificationList(List<ImgInfo> ImgList)
        {
            List<ImgInfo> TempList = new List<ImgInfo>();
            int UploadCount = 0;
            foreach (ImgInfo SinleImgInfo in ImgList)
            {
                new PrintLog().PercentLog(ImgList.Count, ++UploadCount);
                Console.WriteLine("待写入：" + TempList.Count);

                if (!WebHelper.CheckImgExits(SinleImgInfo.SinaImg1, UploadUtils.BlockImgInfo))
                {
                    Console.WriteLine("已失效{0}，时间：{1}", SinleImgInfo, DateTime.Now.ToString());

                    continue;
                }
                TempList.Add(SinleImgInfo);
            }
            return TempList;
        }

        /// <summary>
        /// 批量清理失效Sain链接
        /// </summary>
        /// <param name="FloderPath"></param>
        internal void ClearXmlWithFlode(string FloderPath)
        {
            List<string> FoloderList = Directory.GetFiles(FloderPath, "*.*",
                    SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".lst") || s.EndsWith(".txt") || s.EndsWith(".acg")).ToList();
            foreach (string line in FoloderList)
            {
                Console.WriteLine(line);
                ClearXmlWithFile(line);
            }
        }

        /// <summary>
        /// 清理单个文件失效Sain链接
        /// </summary>
        /// <param name="FilePath"></param>
        public void ClearXmlWithFile(string FilePath)
        {
            string FileContent = TextHelper.ReplaceTextEnhance(FileHelper.ReadAllText(FilePath), "sinaimg.cn/.*?/", "sinaimg.cn/large/");
            List<string> ImgList = HtmlHelper.GetHtmlValueList(FileContent, "//bed");
            List<string> CompltedList = new List<string>();
            int UploadCount = 0;

            foreach (string SinleImgInfo in ImgList)
            {
                new PrintLog().PercentLog(ImgList.Count, ++UploadCount);
                if (SinleImgInfo == "")
                    continue;
                string SinaImg = TextHelper.ReplaceTextEnhance(HtmlHelper.GetFirstText(SinleImgInfo, "//bed/sina"), "\\(.*?\\)", "").Replace("/large/", "/orj360/").Replace("https", "http");
                string PixivImg = TextHelper.ReplaceTextEnhance(HtmlHelper.GetFirstText(SinleImgInfo, "//bed/original"), "\\(.*?\\)", "");
                int DataBaseTemp = new PicBedManger().SinaImgInvalid(SinaImg);
                Console.WriteLine("数据库返回缓存" + DataBaseTemp);
                switch (DataBaseTemp)
                {
                    case 0:
                        Console.WriteLine("数据库缓存失效。");
                        break;

                    case 1:
                        Console.WriteLine("数据库缓存有效。");
                        CompltedList.Add(SinleImgInfo);
                        break;

                    default:
                        Console.WriteLine("数据库无缓存，检查中。");
                        if (!WebHelper.CheckImgExits(SinaImg, UploadUtils.BlockImgInfo))
                        {
                            new PicBedManger().UpdateValue(PixivImg, SinaImg, true.ToString());

                            Console.WriteLine("已失效{0}，时间：{1}", SinleImgInfo, DateTime.Now.ToString());
                        }
                        else
                        {
                            CompltedList.Add(SinleImgInfo);
                            new PicBedManger().UpdateValue(PixivImg, SinaImg, "exits");
                        }
                        break;
                }
            }
            if (CompltedList.Count > 0)
                FileHelper.WriteList(FileNameHelper.CreateFileNameWithChildFloder(FilePath, "已检查图片有效性"), CompltedList);
        }
    }
}